#!/bin/bash
current_path=$(pwd)
log_path=$current_path/log

################环境信息收集#######################
collect_os_msg(){
    echo  os: $(cat /etc/os-release |grep PRETTY_NAME=|awk -F= '{print $2}') >  $log_path/os_msg.log
    echo kernel: $(uname -r) >> $log_path/os_msg.log
    dmidecode -t Processor|grep -m 1 Version: >> $log_path/os_msg.log
    dmidecode -t system|grep "Product Name" >> $log_path/os_msg.log
  }

####################打包日志文件############################
tar_log_file(){
  datatime=$(date '+%Y%m%d%H%M')

  if test -d $log_path;
  then
      echo "customer_information: BiShengJDK_${datatime}.tar.gz" >> $log_path/os_msg.log
      tar zcvf BiShengJDK_${datatime}.tar.gz -C $log_path .
      echo "The generated report is in the current path. The file "${current_path}/BiShengJDK_${datatime}.tar.gz
  else
      echo "$log_path logpath abnormality, please check"
  fi
}

################毕昇JDK信息收集#######################
collect_bisheng_jdk_heap(){
    mkdir -p $log_path
    check_sysbol=`objdump -tT /usr/lib64/libjvm*_kunpeng.so | grep HeapDict_Add`
    if [[ $check_sysbol = '' ]];then
       echo "failed" > $log_path/bisheng_jdk_heap.log
       return
    fi

    jmap_cmd=$1"/bin/jmap"
    pid=$2
    dump_file="/tmp/"${pid}"_dump.hprof"
    $jmap_cmd -dump:format=b,file=$dump_file,HeapDumpRedact=names,RedactMap="value:#**#" $pid > $log_path/bisheng_jdk_heap.log

    rm -rf $dump_file
}


collect_bisheng_jdk_jbooster(){
    readelf -Ws /usr/lib64/libjvm17_kunpeng.so | grep JBooster > $log_path/bisheng_jdk_jbooster.log
}


collect_bisheng_jdk_jbolt(){
    readelf -Ws /usr/lib64/libjvm*_kunpeng.so | grep JBolt > $log_path/bisheng_jdk_jbolt.log
}


collect_bisheng_jdk_kae_provider(){
    kae_log_path=$1
    cp $kae_log_path $log_path/bisheng_jdk_kae_provider.log
}


collect_bisheng_jdk_kae_zlib(){
    kae_zlib_pid=$1
    pmap -p $kae_zlib_pid > $log_path/bisheng_jdk_kae_zlib.log
}


collect_bisheng_jdk_g1gc(){
    ps -ef| grep java > $log_path/bisheng_jdk_g1gc.log
}

packing_logs(){
  cd $log_path
  tar -czf ../BiShengJDK.tar.gz *
  echo "The generated report is in the current path. The file "${current_path}"/BiShengJDK.tar.gz"
}

bisheng_jdk_dir=""
bisheng_jdk_heap_pid=""
kae_log_path=""
kae_zlib_pid=""

while [[ "$#" -gt 0 ]]; do
    case $1 in
        -bisheng_jdk_dir) bisheng_jdk_dir="$2"; shift ;;
        -bisheng_jdk_heap_pid) bisheng_jdk_heap_pid="$2"; shift ;;
        -kae_log_path) kae_log_path="$2"; shift ;;
        -kae_zlib_pid) kae_zlib_pid="$2"; shift ;;
        *) echo "未知参数: $1"; exit 1 ;;
    esac
    shift
done

collect_bisheng_jdk_heap $bisheng_jdk_dir $bisheng_jdk_heap_pid
collect_bisheng_jdk_jbooster
collect_bisheng_jdk_jbolt
collect_bisheng_jdk_kae_provider $kae_log_path
collect_bisheng_jdk_kae_zlib $kae_zlib_pid
collect_bisheng_jdk_g1gc
collect_os_msg
tar_log_file
